package com.duan.SpringbootMybatis.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.UUID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.RandomStringUtils;

/**
 * 
 * @ClassName: DataUtil
 * @Description: TODO(开发帮助类)
 * @author duanws
 * @date 2017-3-12 下午03:09:29
 *
 */
public class DataUtil {
	/**
	 * 
	 * @Title: strIsNull
	 * @Description: TODO(判断是字符串是否为空)
	 * @param @param str
	 * @param @return
	 * @return boolean
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午2:46:55
	 */
	public static boolean strIsNull(String str) {
		return (str == null) || (str.trim().length() == 0);
	}

	/**
	 * 
	 * @Title: strIsNotNull
	 * @Description: TODO(判断字符串不为空)
	 * @param @param str
	 * @param @return
	 * @return boolean
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午2:48:22
	 */
	public static boolean strIsNotNull(String str) {
		return !strIsNull(str);
	}

	/**
	 * 
	 * @Title: ObjIsNotNull
	 * @Description: TODO(判断对象不为空)
	 * @param @param obj
	 * @param @return
	 * @return boolean
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午2:55:47
	 */
	public static boolean objIsNotNull(Object obj) {
		if (obj != null) {
			return !strIsNull(obj.toString());
		}
		return false;
	}

	/**
	 * 
	 * @Title: strToLong
	 * @Description: TODO(字符串转long型)
	 * @param @param str
	 * @param @return
	 * @return long
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午2:57:50
	 */
	public static long strToLong(String str) {
		int r = 0;
		if (strIsNull(str)) {
			return r;
		}
		return Long.parseLong(str.trim());
	}

	/**
	 * 
	 * @Title: strToDouble
	 * @Description: TODO(字符串转Double型)
	 * @param @param str
	 * @param @return
	 * @return double
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午3:00:32
	 */
	public static double strToDouble(String str) {
		double r = 0.0D;
		if (strIsNull(str)) {
			return r;
		}
		return Double.parseDouble(str.trim());
	}

	/**
	 * 
	 * @Title: strToFloat
	 * @Description: TODO(字符串转Float型)
	 * @param @param str
	 * @param @return
	 * @return float
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午3:03:25
	 */
	public static float strToFloat(String str) {
		float r = 0.0F;
		if (strIsNull(str)) {
			return r;
		}
		return Float.parseFloat(str.trim());
	}

	/**
	 * 
	 * @Title: strToInt
	 * @Description: TODO(字符串转int型)
	 * @param @param str
	 * @param @return
	 * @return int
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午3:08:58
	 */
	public static int strToInt(String str) {
		if (strIsNull(str)) {
			return 0;
		}
		try {
			Integer r = new Integer(str);
			return r.intValue();
		} catch (Exception e) {
		}
		return 0;

	}

	/**
	 * 
	 * @Title: strSplit
	 * @Description: TODO(字符串截取到数组中，二维)
	 * @param @param str
	 * @param @param flag1
	 * @param @param flag2
	 * @param @return
	 * @return String[][]
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午3:44:19
	 */
	public static String[][] strSplit(String str, String flag1, String flag2) {
		String[] s = strSplit(str, flag1);
		String[][] res = (String[][]) null;
		if (s != null) {
			res = new String[s.length][];
			for (int i = 0; i < s.length; ++i) {
				String[] s1 = strSplit(s[i], flag2);
				res[i] = s1;
			}
		}
		return res;
	}

	/**
	 * 
	 * @Title: strSplit
	 * @Description: TODO(字符串截取到数组中，一维)
	 * @param @param str
	 * @param @param flag
	 * @param @return
	 * @return String[]
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午3:43:56
	 */
	public static String[] strSplit(String str, String flag) {
		if (strIsNull(str)) {
			return null;
		}
		if (str.indexOf(flag) == -1) {
			return new String[] { str };
		}

		ArrayList res = new ArrayList();
		int sIndex = 0;
		int eIndex = 0;
		while (true) {
			eIndex = str.indexOf(flag, sIndex);
			if (eIndex == -1) {
				break;
			}
			if (eIndex == 0) {
				res.add("");
			} else {
				res.add(str.substring(sIndex, eIndex).trim());
			}
			sIndex = eIndex + str.lastIndexOf(flag);
		}
		int lastIndex = str.lastIndexOf(flag);
		if (lastIndex >= 0) {
			res.add(str.substring(lastIndex + flag.length()).trim());
		} else if (lastIndex == str.length() - 1) {
			res.add("");
		}
		String[] data = toArray(res);

		return data;
	}

	/**
	 * 
	 * @Title: toArray
	 * @Description: TODO(列表转数组)
	 * @param @param res
	 * @param @return
	 * @return String[]
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午3:43:09
	 */
	public static String[] toArray(ArrayList res) {
		if (!objIsNotNull(res)) {
			return null;
		}
		String[] ary = new String[res.size()];
		res.toArray(ary);
		return ary;
	}

	/**
	 * 
	 * @Title: strIsNumber
	 * @Description: TODO(判断字符中是否都为数字)
	 * @param @param str
	 * @param @return
	 * @return boolean
	 * @throws
	 * @author duanws
	 * @date 2016-3-17 下午3:49:14
	 */
	public static boolean strIsNumber(String str) {
		if (strIsNull(str)) {
			return false;
		}
		String strNumber = "0123456789";

		String oneStr = str.substring(0, 1);
		if ((oneStr.equals("-")) || (oneStr.equals("+"))) {
			str = str.substring(1);
		}

		String[] aStr = strSplit(str, ".");
		if (aStr.length > 2) {
			return false;
		}
		for (int i = 0; i < aStr.length; ++i) {
			for (int j = 0; j < aStr[i].length(); ++j) {
				if (strNumber.indexOf(aStr[i].substring(j, j + 1)) == -1) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * 
	 * @Title: getRandomInt
	 * @Description: TODO(获得整数随机数)
	 * @param @param start
	 * @param @param end
	 * @param @return
	 * @return int
	 * @throws
	 * @author duanws
	 * @date 2016-3-18 上午10:33:27
	 */
	public static int getRandomInt(int start, int end) {
		Random r = new Random();
		int n = 0;
		while (n < start) {
			n = r.nextInt(end);
		}
		return n;
	}

	/**
	 * 
	 * @Title: getRandomString
	 * @Description: TODO(获得随机字符串)
	 * @param @param leng 字符串长度
	 * @param @param letters 如果为true则包含字母
	 * @param @param numbers 如果为true则包含数字
	 * @param @return
	 * @return String
	 * @throws
	 * @author duanws
	 * @date 2016-3-18 上午10:45:07
	 */
	public static String getRandomString(int leng, boolean letters,
			boolean numbers) {
		return RandomStringUtils.random(leng, letters, numbers);
	}

	/**
	 * 
	 * @Title: getUUID
	 * @Description: TODO(获得32位随机id)
	 * @param @return
	 * @return String
	 * @throws
	 * @author duanws
	 * @date 2016-3-18 下午2:23:11
	 */
	public static String getUUID() {
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		return uuid;
	}

	/**
	 * 
	 * @Title: arrayToJson
	 * @Description: TODO(list转成json字符串)
	 * @param @param list
	 * @param @return
	 * @return String
	 * @throws
	 * @author duanws
	 * @date 2016-3-18 下午2:40:28
	 */
	public static String arrayToJson(List list) {
		return JSONArray.fromObject(list).toString();
	}

	/**
	 * 
	 * @Title: arrayToJson
	 * @Description: TODO(数组转json字符串)
	 * @param @param array
	 * @param @return
	 * @return String
	 * @throws
	 * @author duanws
	 * @date 2016-3-18 下午2:46:25
	 */
	public static String arrayToJson(Object[] array) {
		return JSONArray.fromObject(array).toString();
	}

	/**
	 * 
	 * @Title: beanToJson
	 * @Description: TODO(bean转json字符串)
	 * @param @param obj
	 * @param @return
	 * @return String
	 * @throws
	 * @author duanws
	 * @date 2016-3-18 下午2:47:25
	 */
	public static String beanToJson(Object obj) {
		return JSONObject.fromObject(obj).toString();
	}

	/**
	 * 
	 * @Title: mapToJson
	 * @Description: TODO(map转json字符)
	 * @param @param map
	 * @param @return
	 * @return String
	 * @throws
	 * @author duanws
	 * @date 2016-3-18 下午2:49:50
	 */
	public static String mapToJson(Map map) {
		return beanToJson(map);
	}

	/**
	 * 
	 * @Title: getDataFormatStr
	 * @Description: TODO(返回直指格式的日期字符串)
	 * @param @param d
	 * @param @param format 日期格式
	 * @param @return
	 * @return String
	 * @throws
	 * @author duanws
	 * @date 2016-3-18 下午3:18:11
	 */
	public static String getDataFormatStr(Date d, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (!objIsNotNull(d)) {
			Date today = new Date();
			return sdf.format(today);
		}
		return sdf.format(d);
	}

	/**
	 * 
	 * @Title: getCharFormStr
	 * @Description: TODO(返回字符串指定位置的字符)
	 * @param @param str
	 * @param @param index
	 * @param @return
	 * @return String
	 * @throws
	 * @author duanws
	 * @date 2016-3-28 上午10:21:29
	 */
	public static String getCharFormStr(String str, int index) {
		if (str.length() < 1) {
			return str;
		}
		return String.valueOf(str.charAt(index));
	}

	/**
	 * 
	 * @Title: changeMapToStr
	 * @Description: TODO(map转成字符串)
	 * @param @param map
	 * @param @return
	 * @return String
	 * @author duanws
	 * @throws
	 */
	public static String changeMapToStr(Map<Object, Object> map) {
		if (map.size() < 1) {
			return null;
		}

		StringBuilder mapStr = new StringBuilder();
		for (Object key : map.keySet()) {
			mapStr.append(key + ":" + map.get(key) + ",");
		}
		return mapStr.toString().substring(0, mapStr.length() - 1);
	}

	/**
	 * 
	 * @Title: getPingIpResult
	 * @Description: TODO(测试ip是否ping通)
	 * @param @param ipStr
	 * @param @return
	 * @return boolean
	 * @author duanws
	 * @date 2017年3月1日 下午4:48:10
	 * @throws
	 */
	public static boolean getPingIpResult(String ipStr) {
		int timeOut = 3000; // 超时设置为3钞
		boolean status = false;
		if (DataUtil.strIsNotNull(ipStr)) {
			try {
				status = InetAddress.getByName(ipStr).isReachable(timeOut);
			} catch (UnknownHostException e) {

			} catch (IOException e) {
			}
		}
		return status;
	}
	  /**
     *  
    * @Title: getNextWorkDay 
    * @Description: TODO(获得下一个工作日) 
    * @param @param dateStr
    * @param @return
    * @return String 
    * @author duanws
    * @date 2017年3月21日 下午6:00:10
    * @throws
     */
	public static String getNextWorkDay(String dateStr){
		String nextWorkDay = "";
		String checkWorkDay = "";
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		try {
			calendar.setTime(sdf.parse(dateStr));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		calendar.add(Calendar.DAY_OF_YEAR, 1);
		Date date = calendar.getTime();
		checkWorkDay = checkWorkDay(sdf.format(date));
		
		while(!"0".equals(checkWorkDay)){
			calendar.add(Calendar.DAY_OF_YEAR, 1);
			date = calendar.getTime();
			checkWorkDay = checkWorkDay(sdf.format(date));
		}
		nextWorkDay = sdf.format(date);
		return nextWorkDay;
	}
	/**
	 * 
	* @Title: checkWorkDay 
	* @Description: TODO(判断日期是否是工作日) 
	* @param @param dateStr
	* @param @return
	* @return String 
	* @author duanws
	* @date 2017年3月21日 下午6:20:10
	* @throws
	 */
	public static String checkWorkDay(String dateStr){
		String resultStr = "";
		BufferedReader reader = null;
	    String result = null;
	    StringBuffer sbf = new StringBuffer();
	    String httpUrl = "http://tool.bitefu.net/jiari/";
	    httpUrl = httpUrl + "?d=" + dateStr;
	    
	    String proxyHost="10.81.1.56";
		String proxyPort="3128";
//		String proxyUserAndPassword=MfhcdConstants.PROXY_USERANDPASSWORD.replace(",",":");
	    try {
	       URL url = new URL(httpUrl);
	    // 创建代理服务器  
           InetSocketAddress addr = new InetSocketAddress(proxyHost,Integer.parseInt(proxyPort));  
           Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理  
	       //添加代理
	       HttpURLConnection connection = (HttpURLConnection) url
	               .openConnection(proxy);
//	       HttpURLConnection connection = (HttpURLConnection) url
//	               .openConnection();
	       connection.setRequestMethod("GET");
	       connection.connect();
	       InputStream is = connection.getInputStream();
	       reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
	       String strRead = null;
	       while ((strRead = reader.readLine()) != null) {
	           sbf.append(strRead);
	       }
	       reader.close();
	       result = sbf.toString().substring(sbf.toString().length()-1, sbf.toString().length());;
	    } catch (Exception e) {
	       e.printStackTrace();
	    }
		return result;
	}
	/**
	 * 
	* @Title: getNextDate 
	* @Description: TODO(获得下一年或下一月或下一天) 
	* @param @param mark  year年  month月  day天
	* @param @param nowDate
	* @param @param num 增加的数量
	* @param @return
	* @return Date 
	* @author duanws
	* @date 2017年5月4日下午2:31:38
	* @throws
	 */
	public static Date getNextDate(String mark,Date nowDate,int num){
		if(DataUtil.strIsNull(mark)) return nowDate;
		
		Date nextDate = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(nowDate);
		if("year".equals(mark.toLowerCase())){
			calendar.add(Calendar.YEAR, num);
		}else if("month".equals(mark.toLowerCase())){
			calendar.add(Calendar.MONTH, num);
		}else if("day".equals(mark.toLowerCase())){
			calendar.add(Calendar.DATE, num);
		}
		nextDate = calendar.getTime();
		
		return nextDate;
	}
	/**
	 * 
	* @Title: mapToObject 
	* @Description: TODO( map转 bean) 
	* @param @param map
	* @param @param beanClass
	* @param @return
	* @param @throws Exception
	* @return Object 
	* @author duanws
	* @date 2017年9月25日上午9:56:24
	* @throws
	 */
    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
        if (map == null)  
            return null;  
  
        Object obj = beanClass.newInstance();  
  
        org.apache.commons.beanutils.BeanUtils.populate(obj, map);  
  
        return obj;  
    }    
    /**
     *   
    * @Title: objectToMap 
    * @Description: TODO(bean转map) 
    * @param @param obj
    * @param @return
    * @return Map<?,?> 
    * @author duanws
    * @date 2017年9月25日上午9:56:44
    * @throws
     */
    public static Map<?, ?> objectToMap(Object obj) {  
        if(obj == null)  
            return null;   
  
        return new org.apache.commons.beanutils.BeanMap(obj);  
    }    
    /**
	 * 
	* @Title: getPreHourRange 
	* @Description: TODO(返回上一个小时，时间段) 
	* @param @param date
	* @param @return
	* @return Map<String,String> 
	* @author duanws
	* @date 2017年9月7日上午10:07:15
	* @throws
	 */
	public static Map<String,String> getPreHourRange(Date date){
		Map<String,String> timeMap = new HashMap<String,String>();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, 00);
		calendar.set(Calendar.SECOND, 00);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
		String rangeStrat = sdf.format(calendar.getTime());
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		String rangeEnd = sdf.format(calendar.getTime());
		//日期
		timeMap.put("profitDate", rangeStrat.substring(0,8));
		//时间开始时间
		timeMap.put("rangeStrat", rangeStrat.substring(9,15));
		//时间结束时间
		timeMap.put("rangeEnd", rangeEnd.substring(9,15));		
		return timeMap;
	}  
	
	/**
	 * @throws ParseException 
	 * 
	 * @Title: main
	 * @Description: TODO(工具类实验)
	 * @param @param args
	 * @return void
	 * @author duanws
	 * @throws
	 */
//	public static void main(String[] args) throws ParseException {
//		String datestr = "20170504";
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//		Date date = DataUtil.getNextDate("Month",sdf.parse(datestr),2);
//		
//		System.out.println(sdf.format(date));
//	}
}
